﻿using System;
using System.Collections;
using Huirui.Cavan.Presentation.Seedwork;
using Huirui.Cavan.Domain.MainBoundedContext.PurchaseModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Presentation.ViewModel.PurchaseModule;
using Huirui.Cavan.Application.MainBoundedContext.PurchaseModule;
using System.Web.Mvc;
using Huirui.Cavan.Core.Extension;
using Huirui.Cavan.Domain.Seedwork;
using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.MainBoundedContext.StockModule;


namespace Huirui.Cavan.Presentation.Controller.PurchaseModule
{
    #region Purchase

    public class PurchaseController : EntityController<Purchase, string, PurchaseSearch, IPurchaseViewModel, IPurchaseAppService>, IPurchaseController
    {
        private ICurrencyAppService CurrencyAppService { get; set; }
        public override string ControllerName
        {
            get { return ControllerConst.PURCHASE ; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        public PurchaseController()
        {
            this.Model = ModelFactory.CreateModel<IPurchaseViewModel>();
        }

        [ActionName(ActionConst.ADD)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "purchase_query")]
        public ActionResult AddPurchase()
        {
            this.Model.Item = new Purchase();
            this.Model.Item.Currency = "CNY";
            this.Model.Item.PurchaseDate = DateTime.Now;
            IList<Currency> currencies = this.CurrencyAppService.GetBySearch(new CurrencySearch()).ToList();
            this.Model.Currencies.Add(new SelectListItem() { Text = "请选择", Value = "1" });
            currencies.ForEach(c =>
            {
                this.Model.Currencies.Add(new SelectListItem() { Text = c.Code, Value = c.Rate.ToString() });
                if (c.IsLocal)
                {
                    this.Model.Currencies.FirstOrDefault(c1 => c1.Text == c.Code).Selected = true;
                    this.Model.Item.CurrencyRate = c.Rate;
                }
            });

            return View("~/Views/Purchase/v1/addPurchase.cshtml", this.Model);
        }

        [ActionName(ActionConst.ADD)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "purchase_edit")]
        public JsonResult AddPurchase(PurchaseViewModel model)
        {
            BoolResult jsonResult = null;
            model.Item.StockInOut = new StockInOut();
            IList<PurchaseOrderJson> purchaseOrders = JsonConvert.DeserializeObject<IList<PurchaseOrderJson>>(model.PurchaseOrders);
            IList<StockInOutDetail> stockInOutDetails = JsonConvert.DeserializeObject<IList<StockInOutDetail>>(model.StockInOutDetails);
            model.Item.StockInOut.StockInOutDetails = stockInOutDetails;
            foreach (PurchaseOrderJson po in purchaseOrders)
            {
                model.Item.PurchaseOrder = new PurchaseOrder() { Id = po.Id };
                model.Item.StockInOut.StockInOutDetails = stockInOutDetails.Where(s => s.Description == po.Id).ToList();
                if (model.Item.StockInOut.StockInOutDetails.Count > 0)
                {
                    AddPurchaseResult result = (AddPurchaseResult)this.AppService.AddOrUpdate(model.Item);
                    jsonResult = new BoolResult(result.IsSuccess, result.Messages);
                }
            }

            return Json(jsonResult, "application/json");
        }
    }
    #endregion
}
